package com.fs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.entity.*;
import com.fs.mapper.DepartmentMapper;
import com.fs.mapper.MajorMapper;
import com.fs.mapper.StudentsMapper;
import com.fs.service.IStudentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.util.StudentsContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-12
 */
@Service
public class StudentsServiceImpl  implements IStudentsService {

    @Autowired
    StudentsMapper studentsMapper;
    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    MajorMapper majorMapper;

    @Override
    public void login(int sid, String password) {
        // 根据学号查询 所属学生对象
        Students students = studentsMapper.selectById(sid);
        //判断 该学生是否存在 不存在需要注册 （跳转到注册页面？）
        if (students != null) {
            if (password.equals(students.getPassword())) {
                //保存到session中
                StudentsContext.setStudents(students);
                System.out.println("登录成功");
                return;
            }
            else {
                System.out.println("密码错误");
            }
        }

    }

    @Override
    public boolean register(Students students) {
        return studentsMapper.insert(students) > 0 ? true : false;
    }

    @Override
    public boolean modify(Students students) {
        int row=0;
        //需要判断是否已经登录
        //System.out.println(StudentsContext.getStudents());
        if(StudentsContext.getStudents()!=null)
        {
            //用户已经登录
            row=studentsMapper.updateById(students);
        }
//        else {
//           login(students.getSid(),students.getPassword());
//        }
        return row>0? true:false;
    }

    @Override
    public int queryBySid(Integer sid) {
        int crd=0;
        //还是需要判定是否已经登录
        if(StudentsContext.getStudents()!=null){
            //用户登录 开始执行查询功能
            crd= studentsMapper.selectById(sid).getCredit();
            System.out.println(crd);
        }
        return crd;
    }

    @Override
    public List<Materials> getMaterialsUnderReviewBySid(Integer sid) {
        List<Materials> materials=new ArrayList<>();
        //还是需要登录后进行操作
        if(StudentsContext.getStudents()!=null) {
            materials= studentsMapper.getMaterialsUnderReviewBySid(sid);
            // System.out.println(materials);
        }
        return materials;
    }
    @Override
    public Page queryByPageAndCondition(StuCondition stuCondition) {
        Page<StuShow> page = Page.of(1, 6);
        if (stuCondition.getCurrentpage() != null) {
            page.setCurrent(stuCondition.getCurrentpage());
        }

        Page <StuShow>stuPage=studentsMapper.selectStuPageWithConditions(page,stuCondition);
        System.out.println(stuPage.getRecords());
        return stuPage;
    }

    @Override
    public Boolean updateStuCredit(Integer[] sids, Integer credit) {
        UpdateWrapper<Students> updateWrapper=new UpdateWrapper<>();
        updateWrapper.setSql("credit=credit+"+credit).in("sid", Arrays.asList(sids));
        int rs = studentsMapper.update(null, updateWrapper);
        if(rs!=0)
        {
            return true;
        }
        return false;
    }

    @Override
    public List<Materialcategory> selectMaterialCategoriesWithoutStudentSubmissions(Integer sid) {
        //从登录的用户中去拿到sid
        if (StudentsContext.getStudents()!=null){
            sid=StudentsContext.getStudents().getSid();
            System.out.println(sid);
        }
        else {
            System.out.println("请先登录");
        }
        List<Materialcategory> materialcategories=new ArrayList<>();
        materialcategories=studentsMapper.selectMaterialCategoriesWithoutStudentSubmissions(sid);
        return materialcategories;
    }

    @Override
    public Page selectMaterialsByConditions(StudentCondition condition,Integer sid) {
        //从session中获取sid
         if(StudentsContext.getStudents()!=null){
            sid=StudentsContext.getStudents().getSid();
            System.out.println(sid);
        }
         else {
             System.out.println("请先登录");
         }
        Page<StuScShow> page = Page.of(1, 3);
        if (condition.getCurrentpage() != null) {
            page.setCurrent(condition.getCurrentpage());
        }

        Page <StuScShow> stuScPage=studentsMapper.selectMaterialsByConditions(page,condition,sid);
        System.out.println(stuScPage.getRecords());
        return stuScPage;
    }

}
